package com.example.forum.controller;


import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Board;
import com.example.forum.services.IBoradService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/board")
public class BoradController {
    // 从配置文件中读取值 ，如果没有配置，默认值为9
    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Autowired
    private IBoradService boradService;



    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("要获取的板块数的数量是："+ indexBoardNum);
        List<Board> boards = boradService.selectByNum(indexBoardNum);
        return AppResult.success(boards);

    }

    @GetMapping("/getById")
    public AppResult<Board> getById(@NonNull Long id){
        Board board = boradService.selectById(id);
        if(board==null||board.getDeleteState()==1){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed("没有id对应的板块值"));
        }
        return AppResult.success(board);


    }
}
